{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0",
   "metadata": {},
   "source": [
    "<a href=\"https://githubtocolab.com/gee-community/geemap/blob/master/docs/notebooks/21_export_map_to_html_png.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open in Colab\"/></a>\n",
    "\n",
    "Uncomment the following line to install [geemap](https://geemap.org) if needed."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# !pip install geemap"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2",
   "metadata": {},
   "source": [
    "# Google Earth Engine Python Tutorials\n",
    "\n",
    "* GitHub: https://github.com/gee-community/geemap\n",
    "* Notebook examples: https://github.com/gee-community/geemap/blob/master/examples/README.md#tutorials\n",
    "* Video tutorials: https://www.youtube.com/playlist?list=PLAxJ4-o7ZoPccOFv1dCwvGI6TYnirRTg3\n",
    "\n",
    "\n",
    "**Tutorial 21  - How to export Earth Engine maps as HTML and images**"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3",
   "metadata": {},
   "source": [
    "## Import libraries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4",
   "metadata": {},
   "outputs": [],
   "source": [
    "import ee\n",
    "import geemap"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5",
   "metadata": {},
   "source": [
    "## Video tutorial on YouTube"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.show_youtube(\"h0pz3S6Tvx0\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7",
   "metadata": {},
   "source": [
    "## Update the geemap package\n",
    "\n",
    "If you run into errors with this notebook, please uncomment the line below to update the [geemap](https://github.com/gee-community/geemap#installation) package to the latest version from GitHub. \n",
    "Restart the Kernel (Menu -> Kernel -> Restart) to take effect."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8",
   "metadata": {},
   "outputs": [],
   "source": [
    "# geemap.update_package()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9",
   "metadata": {},
   "source": [
    "## Create an interactive map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "10",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map(toolbar_ctrl=True, layer_ctrl=True)\n",
    "Map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "11",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Add Earth Engine dataset\n",
    "dem = ee.Image(\"USGS/SRTMGL1_003\")\n",
    "landcover = ee.Image(\"ESA/GLOBCOVER_L4_200901_200912_V2_3\").select(\"landcover\")\n",
    "landsat7 = ee.Image(\"LANDSAT/LE7_TOA_5YEAR/1999_2003\")\n",
    "states = ee.FeatureCollection(\"TIGER/2018/States\")\n",
    "\n",
    "# Set visualization parameters.\n",
    "vis_params = {\n",
    "    \"min\": 0,\n",
    "    \"max\": 4000,\n",
    "    \"palette\": [\"006633\", \"E5FFCC\", \"662A00\", \"D8D8D8\", \"F5F5F5\"],\n",
    "}\n",
    "\n",
    "# Add Earth Engine layers to Map\n",
    "Map.addLayer(dem, vis_params, \"SRTM DEM\", True, 0.5)\n",
    "Map.addLayer(landcover, {}, \"Land cover\")\n",
    "Map.addLayer(\n",
    "    landsat7,\n",
    "    {\"bands\": [\"B4\", \"B3\", \"B2\"], \"min\": 20, \"max\": 200, \"gamma\": 1.5},\n",
    "    \"Landsat 7\",\n",
    ")\n",
    "Map.addLayer(states, {}, \"US States\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "12",
   "metadata": {},
   "source": [
    "## Exporting maps as HTML\n",
    "\n",
    "You can either click the camera icon on toolbar to export maps or use the following script."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "13",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "14",
   "metadata": {},
   "outputs": [],
   "source": [
    "download_dir = os.path.join(os.path.expanduser(\"~\"), \"Downloads\")\n",
    "if not os.path.exists(download_dir):\n",
    "    os.makedirs(download_dir)\n",
    "html_file = os.path.join(download_dir, \"my_map.html\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "15",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map.to_html(filename=html_file, title=\"My Map\", width=\"100%\", height=\"880px\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "16",
   "metadata": {},
   "source": [
    "## Exporting maps as PNG/JPG\n",
    "\n",
    "Make sure you click the fullscreen button on the map to maximum the map."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "17",
   "metadata": {},
   "outputs": [],
   "source": [
    "png_file = os.path.join(download_dir, \"my_map.png\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "18",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map.to_image(filename=png_file, monitor=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "19",
   "metadata": {},
   "outputs": [],
   "source": [
    "jpg_file = os.path.join(download_dir, \"my_map.jpg\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "20",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map.to_image(filename=jpg_file, monitor=1)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
